First Level এবং Second Level Caching এর ব্যবহার

Java Technologies - স্প্রিং বুট জেপিএ (Spring Boot JPA) - Spring Boot JPA এবং Caching
345

Caching হল এক গুরুত্বপূর্ণ কৌশল যা ডাটাবেস অ্যাক্সেসের পারফরম্যান্স উন্নত করতে ব্যবহৃত হয়। Spring Boot JPA তে First Level Cache এবং Second Level Cache দুটি ভিন্ন ধরনের ক্যাশিং ব্যবস্থা রয়েছে। এই দুটি ক্যাশিং ব্যবস্থার মধ্যে পার্থক্য রয়েছে, এবং প্রতিটি ব্যবস্থার নিজস্ব সুবিধা ও ব্যবহার রয়েছে।

১. First Level Cache

First Level Cache হল Hibernate (JPA) এর ডিফল্ট ক্যাশ, যা Session বা EntityManager এর সাথে সম্পর্কিত থাকে। যখন কোনো ডাটাবেস রেকর্ড প্রথমবারের মতো লোড করা হয়, তখন তা Hibernate এর First Level Cache তে সঞ্চিত থাকে। এর পরবর্তী যে কোনো রিকোয়েস্টে একই Entity-এর জন্য ডাটাবেস থেকে পুনরায় ডেটা লোড করার প্রয়োজন হয় না; Hibernate প্রথমবারের মতো লোড হওয়া Entity টিকে ক্যাশ থেকে রিটার্ন করে।

First Level Cache এর বৈশিষ্ট্য:

  1. Session Scoped: এটি Hibernate সেশন বা EntityManager এর সাথে সম্পর্কিত থাকে।
  2. Automatic: Hibernate প্রথমবারের মতো লোড হওয়া Entity ক্যাশ করে এবং স্বয়ংক্রিয়ভাবে ক্যাশ থেকে রিটার্ন করে।
  3. Cannot Be Disabled: First Level Cache Hibernate এ স্বয়ংক্রিয়ভাবে সক্রিয় থাকে এবং এটি নিষ্ক্রিয় করা সম্ভব নয়।

First Level Cache উদাহরণ:

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    
    // Getters and Setters
}

@Repository
public class EmployeeRepository {
    
    @Autowired
    private EntityManager entityManager;

    public Employee getEmployeeById(Long id) {
        // First request will load the employee and store it in the First Level Cache
        Employee employee1 = entityManager.find(Employee.class, id);
        
        // Second request will fetch the employee from First Level Cache (no DB query)
        Employee employee2 = entityManager.find(Employee.class, id);
        
        return employee1;
    }
}

এখানে, entityManager.find() মেথডের মাধ্যমে প্রথমবার Employee Entity লোড হওয়ার পর, এটি First Level Cache তে সঞ্চিত হয়ে থাকে এবং দ্বিতীয়বার একই Entity এর জন্য কোনো নতুন ডাটাবেস কুয়েরি চালানো হয় না।


২. Second Level Cache

Second Level Cache হল Hibernate এর আরও উন্নত ক্যাশিং ব্যবস্থা, যা SessionFactory এর সাথে সম্পর্কিত থাকে। এটি শুধুমাত্র একসাথে একাধিক সেশনকে এক্সেস করতে সক্ষম হয় এবং First Level Cache এর বাইরে ডেটার ক্যাশিং করার জন্য ব্যবহৃত হয়। এর মাধ্যমে বিভিন্ন সেশনের মধ্যে ডেটা শেয়ার করা সম্ভব হয়।

Second Level Cache এর সাথে Hibernate কনফিগার করা যেতে পারে এবং সাধারণত এটি তৃতীয় পক্ষের ক্যাশিং পন্থা যেমন EhCache, Infinispan, Redis ইত্যাদি ব্যবহার করে পরিচালিত হয়।

Second Level Cache এর বৈশিষ্ট্য:

  1. SessionFactory Scoped: এটি Hibernate সেশন ফ্যাক্টরি (SessionFactory) এর সাথে সম্পর্কিত থাকে।
  2. Can Be Configured: Hibernate এর ক্যাশিং কনফিগারেশন ফাইলের মাধ্যমে এটি কাস্টমাইজ করা যায়।
  3. Optional: এটি ডিফল্টভাবে সক্ষম থাকে না এবং Spring Boot বা Hibernate এর মাধ্যমে সেট আপ করতে হয়।
  4. External Cache Provider: এটি তৃতীয় পক্ষের ক্যাশিং পদ্ধতি ব্যবহার করতে পারে যেমন EhCache, Redis ইত্যাদি।

Second Level Cache সেট আপ উদাহরণ:

Spring Boot JPA-তে Second Level Cache কনফিগারেশন:

  1. Dependencies:

pom.xml (Maven) এ EhCache যোগ করুন:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
</dependency>
  1. application.properties ফাইলে Hibernate Cache কনফিগারেশন যোগ করুন:
# Enable second level cache
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=true
  1. Entity Class: আপনার Entity-তে ক্যাশিং সক্ষম করতে @Cacheable অ্যানোটেশন ব্যবহার করুন:
@Entity
@Cacheable(true)  // Enable second level cache
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  // Define cache strategy
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    // Getters and Setters
}

এখানে, @Cacheable(true) অ্যানোটেশন ব্যবহার করে Second Level Cache সক্ষম করা হয়েছে এবং CacheConcurrencyStrategy.READ_WRITE ক্যাশের কনকারেন্সি স্ট্র্যাটেজি নির্ধারণ করা হয়েছে।

  1. Configure EhCache:

src/main/resources/ehcache.xml ফাইলে EhCache কনফিগারেশন নির্ধারণ করুন:

<ehcache xmlns="http://www.ehcache.org/ehcache/xml">
    <cache name="com.example.springbootjpa.model.Employee"
           maxEntriesLocalHeap="1000"
           eternal="false"
           timeToIdleSeconds="600"
           timeToLiveSeconds="1200">
    </cache>
</ehcache>

এখানে EhCache কনফিগারেশন করা হয়েছে, যা Employee Entity এর জন্য ক্যাশ ম্যানেজ করবে।

Second Level Cache এর কার্যকারিতা উদাহরণ:

public void getEmployeeById(Long id) {
    // First request will load the employee and store it in the Second Level Cache
    Employee employee1 = employeeRepository.findById(id).orElseThrow();

    // Second request will fetch the employee from Second Level Cache (no DB query)
    Employee employee2 = employeeRepository.findById(id).orElseThrow();
}

Second Level Cache ব্যবহার করে, প্রথমবার Employee Entity লোড হওয়ার পর, এটি Second Level Cache তে সঞ্চিত হবে। দ্বিতীয়বার একই Entity রিকোয়েস্টে ডাটাবেসে পুনরায় কুয়েরি করা হবে না, এটি সরাসরি ক্যাশ থেকে রিটার্ন হবে।


পারফরম্যান্স অপটিমাইজেশন

  • First Level Cache ডিফল্টভাবে Spring Boot JPA তে সক্ষম থাকে এবং এটি সেশন লেভেল ক্যাশিং করে, যেখানে এক সেশনে একাধিক Entity লোড হলে তাদের মধ্যে পুনরায় ডেটাবেস কুয়েরি না করে ক্যাশ থেকে রিটার্ন করা হয়।
  • Second Level Cache ব্যবহার করার মাধ্যমে আপনি একাধিক সেশনের মধ্যে ডেটা শেয়ার করতে পারবেন এবং ডেটাবেস কুয়েরি কমাতে পারবেন। এটি বিশেষত যখন অনেক বড় অ্যাপ্লিকেশন থাকে এবং অনেক রেকর্ডের সাথে কাজ করতে হয়, তখন কার্যকরী হয়।

সারাংশ

  • First Level Cache Hibernate-এর ডিফল্ট ক্যাশিং মেকানিজম, যা সেশন ভিত্তিক কাজ করে এবং এটি ক্যাশিং স্বয়ংক্রিয়ভাবে পরিচালনা করে।
  • Second Level Cache Hibernate-এর আরও উন্নত ক্যাশিং মেকানিজম, যা SessionFactory ভিত্তিক এবং একাধিক সেশনের মধ্যে ক্যাশ শেয়ার করতে পারে।
  • Second Level Cache চালু করতে EhCache, Infinispan, বা Redis ব্যবহার করা যেতে পারে এবং @Cacheable অ্যানোটেশন ব্যবহার করে এটি Entity ক্লাসে সক্রিয় করা যায়।
  • ক্যাশিং ব্যবহারে পারফরম্যান্স উন্নতি করা সম্ভব, বিশেষত ডেটাবেস কুয়েরি কমাতে এবং রেসপন্স টাইম দ্রুত করতে।

Lazy Loading এবং Eager Loading এর সাথে মিলিয়ে First Level এবং Second Level Caching ব্যবহার করলে Spring Boot JPA অ্যাপ্লিকেশন আরও কার্যকরী এবং স্কেলেবল হয়ে ওঠে।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...